package com.collect.zambia.zhiScene.zhiWidget.zhiDialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.collect.zambia.R;

import java.util.ArrayList;
import java.util.Objects;

public class ZHIDialogWorkAccountId extends Dialog {

    private TextView workAccountIdConfirm;
    private CheckBox workAccountIdSim1;
    private CheckBox workAccountIdSim2;
    private CheckBox workAccountIdSim3;
    private CheckBox workAccountIdSim4;

    private ZHIDialogWorkAccountIdActionClick mZHIDialogWorkAccountIdActionClick;

    private final Context context;
    private final ArrayList<String> mIdList = new ArrayList<>();

    public static void showDialog(Context context, ArrayList<String> list, ZHIDialogWorkAccountIdActionClick onClick) {
        ZHIDialogWorkAccountId dialogAvatar = new ZHIDialogWorkAccountId(context);
        dialogAvatar.setCallback(onClick);
        dialogAvatar.setWorkAccountIdList(list);
        dialogAvatar.show();
    }

    public ZHIDialogWorkAccountId(Context context) {
        super(context, R.style.CustomDialog);
        this.context = context;
    }

    public void setCallback(ZHIDialogWorkAccountIdActionClick onClick) {
        mZHIDialogWorkAccountIdActionClick = onClick;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_work_account_id);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        initView();
        initViewEvent();
    }

    @Override
    public void show() {
        super.show();
        try {
            int count = mIdList.size();
            if (count == 0) {
                workAccountIdSim1.setVisibility(View.INVISIBLE);
                workAccountIdSim2.setVisibility(View.INVISIBLE);
                workAccountIdSim3.setVisibility(View.INVISIBLE);
                workAccountIdSim4.setVisibility(View.INVISIBLE);

            } else if (count == 1) {
                workAccountIdSim1.setVisibility(View.VISIBLE);
                workAccountIdSim2.setVisibility(View.INVISIBLE);
                workAccountIdSim3.setVisibility(View.INVISIBLE);
                workAccountIdSim4.setVisibility(View.INVISIBLE);
                workAccountIdSim1.setText(context.getString(R.string.work_account_id_num, "1"));

            } else if (count == 2) {
                workAccountIdSim1.setVisibility(View.VISIBLE);
                workAccountIdSim2.setVisibility(View.VISIBLE);
                workAccountIdSim3.setVisibility(View.INVISIBLE);
                workAccountIdSim4.setVisibility(View.INVISIBLE);
                workAccountIdSim1.setText(context.getString(R.string.work_account_id_num, "1"));
                workAccountIdSim2.setText(context.getString(R.string.work_account_id_num, "2"));

            } else if (count == 3) {
                workAccountIdSim1.setVisibility(View.VISIBLE);
                workAccountIdSim2.setVisibility(View.VISIBLE);
                workAccountIdSim3.setVisibility(View.VISIBLE);
                workAccountIdSim4.setVisibility(View.INVISIBLE);
                workAccountIdSim1.setText(context.getString(R.string.work_account_id_num, "1"));
                workAccountIdSim2.setText(context.getString(R.string.work_account_id_num, "2"));
                workAccountIdSim3.setText(context.getString(R.string.work_account_id_num, "3"));

            } else if (count == 4) {
                workAccountIdSim1.setVisibility(View.VISIBLE);
                workAccountIdSim2.setVisibility(View.VISIBLE);
                workAccountIdSim3.setVisibility(View.VISIBLE);
                workAccountIdSim4.setVisibility(View.VISIBLE);
                workAccountIdSim1.setText(context.getString(R.string.work_account_id_num, "1"));
                workAccountIdSim2.setText(context.getString(R.string.work_account_id_num, "2"));
                workAccountIdSim3.setText(context.getString(R.string.work_account_id_num, "3"));
                workAccountIdSim4.setText(context.getString(R.string.work_account_id_num, "4"));
            }

            WindowManager.LayoutParams lp = Objects.requireNonNull(getWindow()).getAttributes();
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT; //设置宽度
            lp.width = WindowManager.LayoutParams.MATCH_PARENT; //设置宽度
            lp.gravity = Gravity.CENTER;
            getWindow().setAttributes(lp);

        } catch (Exception e) {
//            e.printStackTrace();
        }
    }

    private void initView() {
        workAccountIdConfirm = findViewById(R.id.workAccountIdConfirm);
        workAccountIdSim1 = findViewById(R.id.workAccountIdSim1);
        workAccountIdSim2 = findViewById(R.id.workAccountIdSim2);
        workAccountIdSim3 = findViewById(R.id.workAccountIdSim3);
        workAccountIdSim4 = findViewById(R.id.workAccountIdSim4);
    }

    public void setWorkAccountIdList(ArrayList<String> list) {
        try {
            mIdList.clear();
            mIdList.addAll(list);
        } catch (Exception e) {
//            e.printStackTrace();
        }
    }

    private void initViewEvent() {

        workAccountIdSim1.setOnCheckedChangeListener((compoundButton, isChecked) -> {
            if (isChecked) {
                workAccountIdSim2.setChecked(false);
                workAccountIdSim3.setChecked(false);
                workAccountIdSim4.setChecked(false);
            }
        });

        workAccountIdSim2.setOnCheckedChangeListener((compoundButton, isChecked) -> {
            if (isChecked) {
                workAccountIdSim1.setChecked(false);
                workAccountIdSim3.setChecked(false);
                workAccountIdSim4.setChecked(false);
            }
        });

        workAccountIdSim3.setOnCheckedChangeListener((compoundButton, isChecked) -> {
            if (isChecked) {
                workAccountIdSim1.setChecked(false);
                workAccountIdSim2.setChecked(false);
                workAccountIdSim4.setChecked(false);
            }
        });

        workAccountIdSim4.setOnCheckedChangeListener((compoundButton, isChecked) -> {
            if (isChecked) {
                workAccountIdSim1.setChecked(false);
                workAccountIdSim2.setChecked(false);
                workAccountIdSim3.setChecked(false);
            }
        });

        workAccountIdConfirm.setOnClickListener(v -> {
            if ((!workAccountIdSim1.isChecked()) &&
                    (!workAccountIdSim2.isChecked()) &&
                    (!workAccountIdSim3.isChecked()) &&
                    (!workAccountIdSim4.isChecked())) {
                Toast.makeText(context, context.getString(R.string.work_account_id_select_title), Toast.LENGTH_SHORT).show();
                return;
            }

            String workAccountId = getCurrentChecked();
            if (TextUtils.isEmpty(workAccountId)) {
                Toast.makeText(context, context.getString(R.string.work_account_id_select_title), Toast.LENGTH_SHORT).show();
                return;
            }
            mZHIDialogWorkAccountIdActionClick.onConfirm(workAccountId);
            dismiss();
        });
    }

    private String getCurrentChecked() {
        String workAccountId = "";
        int count = mIdList.size();
        if ((workAccountIdSim1.getVisibility() == View.VISIBLE) && workAccountIdSim1.isChecked()) {
            if (count > 0) {
                workAccountId = "0-"+ mIdList.get(0);
            }
        } else if ((workAccountIdSim2.getVisibility() == View.VISIBLE) && workAccountIdSim2.isChecked()) {
            if (count > 1) {
                workAccountId = "1-"+ mIdList.get(1);
            }
        } else if ((workAccountIdSim3.getVisibility() == View.VISIBLE) && workAccountIdSim3.isChecked()) {
            if (count > 2) {
                workAccountId = "2-"+ mIdList.get(2);
            }
        } else if ((workAccountIdSim4.getVisibility() == View.VISIBLE) && workAccountIdSim4.isChecked()) {
            if (count > 3) {
                workAccountId = "3-"+ mIdList.get(3);
            }
        }
        return workAccountId;
    }

    public interface ZHIDialogWorkAccountIdActionClick {
        void onConfirm(String workAccountId);
    }
}
